﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApp.BaseDataSvc;
using YY.Common;

namespace WebApp
{
    public partial class EditFeeFrm : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(EditFeeFrm), this);
            var orderId = Request.Params["OrderId"];
            var feeId = Request.Params["FeeId"];
            var feeType = Request.Params["FeeType"];
            if (string.IsNullOrEmpty(orderId) | string.IsNullOrEmpty(feeType))
            {
            //    WebCommon.CreateJs("initParams(" + orderId + "," + feeType + ",'" + feeId + "');", this);
            //}
            //else
            //{
                Response.Write("非法进入");
                Response.End();
            }
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<TB_Fee> GetFee(int id)
        {
            AjaxResult<TB_Fee> result = new AjaxResult<TB_Fee>();
            result.Data = null;
            result.Msg = null;
            try
            {
                result.Data = this.BaseDataProxy.GetFee(id);
            }
            catch (Exception ex)
            {
                result.Data = null;
                result.Msg = ex.Message;
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<int> SaveFee(string id, int orderId, int feeType, string item, string code, string amount, string memo, string op, string opTime, string unit, string target)
        {
            AjaxResult<int> result = new AjaxResult<int>();
            result.Data = 0;
            result.Msg = null;
            bool update = !string.IsNullOrEmpty(id);
            try
            {
                var order = this.BaseDataProxy.GetOrder(orderId);
                TB_User user = Session["User"] as TB_User;
                if (user.role == (int)EnumCommon.SysRoleType.财务员)
                {
                    if (order.isClosed)
                    {
                        result.Msg = "已收款结束单据不能修改！";
                        return result;
                    }
                }
                else
                {
                    if (order.isSubmit)
                    {
                        result.Msg = "已提交财务审核单据不能修改！";
                        return result;
                    }
                }

                TB_Fee fee = new TB_Fee();
                if (update)
                    fee = this.BaseDataProxy.GetFee(Convert.ToInt32(id));
                WebCommon.EvalControlValueToData(fee, "amount", amount);
                WebCommon.EvalControlValueToData(fee, "code", code);
                WebCommon.EvalControlValueToData(fee, "item", item);
                WebCommon.EvalControlValueToData(fee, "memo", memo);
                WebCommon.EvalControlValueToData(fee, "operator", op);
                WebCommon.EvalControlValueToData(fee, "opTime", opTime);
                WebCommon.EvalControlValueToData(fee, "unit", unit);
                WebCommon.EvalControlValueToData(fee, "target", target);

                if (update)
                {
                    this.BaseDataProxy.UpdateFee(fee);
                    result.Data = fee.id;
                }
                else
                {
                    TB_Fee[] fees = this.BaseDataProxy.GetFees(orderId);
                    if (fees.Any(p => p.feeType == (short)feeType && p.item == Convert.ToInt32(item)))
                    {
                        result.Msg = "已存在相同的费目！";
                        return result;
                    }

                    fee.order = orderId;
                    fee.feeType = (short)feeType;
                    fee.createTime = DateTime.Now;

                    int newId = this.BaseDataProxy.AddFee(fee);
                    result.Data = newId;
                }
            }
            catch (Exception ex)
            {
                result.Data = 0;
                result.Msg = ex.Message;
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<bool> DeleteFee(int id, int orderId)
        {
            AjaxResult<bool> result = new AjaxResult<bool>();
            result.Data = false;
            result.Msg = null;
            try
            {
                var order = this.BaseDataProxy.GetOrder(orderId);
                TB_User user = Session["User"] as TB_User;
                if (user.role == (int)EnumCommon.SysRoleType.财务员)
                {
                    if (order.isClosed)
                    {
                        result.Msg = "已收款结束单据不能修改！";
                        return result;
                    }
                }
                else
                {
                    if (order.isSubmit)
                    {
                        result.Msg = "已提交财务审核单据不能修改！";
                        return result;
                    }
                }

                result.Data = this.BaseDataProxy.DeleteFee(id);
            }
            catch (Exception ex)
            {
                result.Data = false;
                result.Msg = ex.Message;
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<bool> VerifyFee(bool isVerify, int id)
        {
            AjaxResult<bool> result = new AjaxResult<bool>();
            result.Data = false;
            result.Msg = null;
            try
            {
                var fee = this.BaseDataProxy.GetFee(id);
                var order = this.BaseDataProxy.GetOrder(fee.order);
                TB_User user = Session["User"] as TB_User;
                if (order.financier != user.id)
                {
                    result.Msg = "对该单据没有审核权限！";
                    return result;
                }
                fee.isVerify = isVerify;
                fee.verifyMan = user.id;
                fee.verifyTime = DateTime.Now;
                result.Data = this.BaseDataProxy.UpdateFee(fee);
            }
            catch (Exception ex)
            {
                result.Data = false;
                result.Msg = ex.Message;
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public int GetCurrentUser()
        {
            try
            {
                return (Session["User"] as TB_User).id;
            }
            catch { }
            return 0;
        }
    }
}
